package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.FeriaDecSorteoResult;

public class FeriaDecSorteoResultDAOImpl extends JdbcDaoSupport implements IFeriaDecSorteoResultDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;
    
    private UpdateGanadores updateGanadores;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByAnioQuery findByAnioQuery;
    
    private FindLugaresQuery findLugaresQuery;
    
    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_FERIA_DEC_SORTEO_RESULT_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(FeriaDecSorteoResult feriaDecSorteoResult) {
		deleteOperation.execute( feriaDecSorteoResult  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public FeriaDecSorteoResult findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(FeriaDecSorteoResult feriaDecSorteoResult) {
		insertOperation.execute( feriaDecSorteoResult );
	}

	public void update(FeriaDecSorteoResult feriaDecSorteoResult) {
		updateOperation.execute( feriaDecSorteoResult );
	}
	
	public void updateGanadores(FeriaDecSorteoResult feriaDecSorteoResult) {
		updateGanadores.execute( feriaDecSorteoResult );
	}

	public FeriaDecSorteoResult findByCriteria(FeriaDecSorteoResult  feriaDecSorteoResult) {
		return findByCriteriaQuery.runQuery(feriaDecSorteoResult);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByAnio(FeriaDecSorteoResult feriaDecSorteoResult) {
		return findByAnioQuery.runQuery(feriaDecSorteoResult);
	}	
	
	public List lstLugaresFeria() {
		return findLugaresQuery.runQuery();
	}

	public FeriaDecSorteoResult findByActiveState(FeriaDecSorteoResult  feriaDecSorteoResult) {
		return findByActiveStateQuery.runQuery(feriaDecSorteoResult);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	updateGanadores = new UpdateGanadores(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findLugaresQuery = new FindLugaresQuery( getDataSource() );
    	findByAnioQuery = new FindByAnioQuery( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_FERIA_DEC_SORTEO_RESULT_T ("
				  + " ID, "				  
				  + " ID_SORTEO, "
				  + " RADICADOS, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "				  
				  + ") VALUES ("
			  	  + " REG_FERIA_DEC_SORTEO_RESUL_SEC.nextVal, "				  
				  + "(SELECT SORTEO.ID FROM REG_FERIA_DEC_SORTEO_T SORTEO INNER JOIN "
                  + " REG_FERIA_DECEMBRINA_FORM_T FER ON SORTEO.ID_FERIA_DECEMBRINA_FORM=FER.ID "
                  + " WHERE AGNO=? AND FER.LUGAR = ?),"
				  + PARAMETER
                  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			compile();
		}
		public void execute(FeriaDecSorteoResult feriaDecSorteoResult) {
			InsertOperation.this.update( new Object[]{								
				feriaDecSorteoResult.getAgno(),
				feriaDecSorteoResult.getLugar(),
				feriaDecSorteoResult.getRadicados(),
				feriaDecSorteoResult.getEstado(),
				feriaDecSorteoResult.getUsuarioCrea(),					
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_FERIA_DEC_SORTEO_RESULT_T SET "					  					  
					  + " ID_SORTEO = (SELECT SORTEO.ID FROM REG_FERIA_DEC_SORTEO_T SORTEO INNER JOIN "
                      + " REG_FERIA_DECEMBRINA_FORM_T FER ON SORTEO.ID_FERIA_DECEMBRINA_FORM=FER.ID "
                      + " WHERE AGNO=? AND FER.LUGAR = ? ),"
                      + " RADICADOS = ? ,"
                      + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters						
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(FeriaDecSorteoResult feriaDecSorteoResult) {
			UpdateOperation.this.update( new Object[]{				
				feriaDecSorteoResult.getAgno(),
				feriaDecSorteoResult.getLugar(),
				feriaDecSorteoResult.getRadicados(),
				feriaDecSorteoResult.getEstado(),
				feriaDecSorteoResult.getUsuarioModifica(),
				feriaDecSorteoResult.getId(),
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateGanadores extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_FERIA_DEC_FORM_VENTA_T SET GANADOR_SORTEO='S'" 
    			 						+ " WHERE RADICADO IN (?) ";
		public  UpdateGanadores(DataSource ds) {
			super(ds,sql);
			//set here required parameters					
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(FeriaDecSorteoResult feriaDecSorteoResult) {
			UpdateGanadores.this.update( new Object[]{				
				feriaDecSorteoResult.getRadicados()
				});
		}
    }

    
    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_DEC_SORTEO_RESULT_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(FeriaDecSorteoResult feriaDecSorteoResult) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_DEC_SORTEO_RESULT_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			FeriaDecSorteoResult feriaDecSorteoResult = new FeriaDecSorteoResult();
				feriaDecSorteoResult.setId(new Long(rs.getLong("ID")));				
				feriaDecSorteoResult.setIdSorteo(new Long(rs.getLong("ID_SORTEO")));
				feriaDecSorteoResult.setRadicados(rs.getString("RADICADOS"));
				feriaDecSorteoResult.setEstado(rs.getString("ESTADO"));
				feriaDecSorteoResult.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				feriaDecSorteoResult.setFechaCrea(rs.getDate("FECHA_CREA"));		
				feriaDecSorteoResult.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				feriaDecSorteoResult.setFechaModifica(rs.getDate("FECHA_MODIFICA"));	
				feriaDecSorteoResult.setLugar(rs.getString("LUGAR"));	
				feriaDecSorteoResult.setAgno(rs.getString("AGNO"));	
			return feriaDecSorteoResult;
		}
    }
	
    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class LugaresOperation extends MappingSqlQuery {
		public LugaresOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ArrayList typeValueList = new ArrayList();
			String registro[] = {"0","0"};
			registro[0]= rs.getString("LUGAR");
			registro[1]= rs.getString("LUGAR");
			typeValueList.add(registro);
			registro = null;
		return typeValueList;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaDecSorteoResult runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (FeriaDecSorteoResult) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_FERIA_DEC_FORM_VENTA = ? "
			  	  	  					//+ " AND ID_SORTEO = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public FeriaDecSorteoResult runQuery(FeriaDecSorteoResult feriaDecSorteoResult) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//feriaDecSorteoResult.getId(),
				//feriaDecSorteoResult.getIdFeriaDecFormVenta(),
				//feriaDecSorteoResult.getIdSorteo(),
				//feriaDecSorteoResult.getEstado(),
				//feriaDecSorteoResult.getUsuarioCrea(),
				//feriaDecSorteoResult.getFechaCrea(),
				//feriaDecSorteoResult.getUsuarioModifica(),
				//feriaDecSorteoResult.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (FeriaDecSorteoResult) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- FindAllQuery ----------------------------------------    
    class FindLugaresQuery extends LugaresOperation {
		private static final String sql = "SELECT LUGAR FROM REG_FERIA_DECEMBRINA_FORM_T";										
    	public FindLugaresQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }   

    // ------------- FindAllQuery ----------------------------------------    
    class FindByAnioQuery extends BaseQueryOperation {
		private static final String sql = "SELECT SR.*,FC.LUGAR,FC.AGNO FROM REG_FERIA_DEC_SORTEO_RESULT_T SR INNER JOIN" 
									   + " REG_FERIA_DEC_SORTEO_T S ON SR.ID_SORTEO=S.ID INNER JOIN "
									   + " REG_FERIA_DECEMBRINA_FORM_T FC ON S.ID_FERIA_DECEMBRINA_FORM = FC.ID " 
										+ WHERE + " FC.AGNO=? ";
    	public FindByAnioQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(FeriaDecSorteoResult feriaDecSorteoResult) {
			List result =  execute( new Object[]{
					feriaDecSorteoResult.getAgno()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaDecSorteoResult runQuery(FeriaDecSorteoResult feriaDecSorteoResult) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (FeriaDecSorteoResult) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }


}
